package com.ijiuchuang.jc_localization.view.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.lite.Tag;
import com.ijiuchuang.jc_localization.contract.DeviceListContract;
import com.ijiuchuang.jc_localization.presenter.DeviceListPresenter;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.DividerItemDecoration;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.OnItemClickListener;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.OnItemLongClickListener;
import com.ijiuchuang.jc_localization.view.adapter.DeviceListRecyclerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class DeviceListFragment extends Fragment implements DeviceListContract.View {


    @BindView(R.id.device_list_recycler)
    RecyclerView mRecyclerView;

    private DeviceListContract.Presenter presenter;
    private DeviceListRecyclerAdapter mRecyclerAdapter;

    @Override
    public void onStart() {
        super.onStart();

        presenter.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device_list, container, false);
        ButterKnife.bind(this, view);

        presenter = new DeviceListPresenter(getActivity(), this);

        return view;
    }

    @Override
    public void setDatas(final List<Tag> datas) {
        mRecyclerAdapter = new DeviceListRecyclerAdapter(getActivity(), R.layout.recycler_item_device_list, datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, final Object o, final int position) {

                final EditText editText = new EditText(getActivity());
                editText.setHint(R.string.hint_changeName);

                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                        .setTitle(R.string.dialog_title_info)
                        .setView(editText)
                        .setPositiveButton(R.string.dialog_btn_submit, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Tag tag = (Tag) o;
                                if (!editText.getText().toString().trim().isEmpty()) {
                                    presenter.changeName(tag, editText.getText().toString().trim());
                                }
                                dialog.dismiss();
                                mRecyclerAdapter.updateData(position);
                            }
                        })
                        .setNegativeButton(R.string.dialog_btn_cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                dialog.show();
            }
        });

        mRecyclerAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public void onItemLongClick(ViewGroup parent, View view, final Object o, final int position) {
                Snackbar.make(view, "Delete?", Snackbar.LENGTH_SHORT)
                        .setAction("OK", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                presenter.deleteData(o);
                                mRecyclerAdapter.removeData(position);
                            }
                        })
                        .show();
            }
        });
    }

    @Override
    public void setPresenter(DeviceListContract.Presenter presenter) {
        this.presenter = presenter;
    }
}
